package com.minelx.woods.data.values;

import java.util.List;
import java.util.Objects;

public final class ReviewValues {
	private final String id;

	private final String name;

	private final String taskId;

	private final String submitterId;

	private final List<String> approved;

	private final List<String> reviewers;

	private final boolean isRejected;

	public ReviewValues(String id,
	                    String name, String taskId,
	                    String submitterId,
	                    List<String> approved,
	                    List<String> reviewers,
	                    boolean isRejected) {
		this.id = id;
		this.name = name;
		this.taskId = taskId;
		this.submitterId = submitterId;
		this.approved = approved;
		this.reviewers = reviewers;
		this.isRejected = isRejected;
	}

	public List<String> getApproved() {
		return approved;
	}

	public List<String> getReviewers() {
		return reviewers;
	}

	public boolean canRejectedBy(String reviewerId) {
		return notEvenApprovedBy(reviewerId)
				&& permitted(reviewerId);
	}

	public boolean canApprovedBy(String reviewerId) {
		return notEvenApprovedBy(reviewerId)
				&& pending()
				&& permitted(reviewerId);
	}

	public boolean notEvenApprovedBy(String reviewerId) {
		return !approved.contains(reviewerId);
	}

	public boolean pending() {
		return !isRejected && !completed();
	}

	public boolean permitted(String reviewerId) {
		return getReviewers().contains(reviewerId);
	}

	public boolean completed() {
		return approved.containsAll(reviewers);
	}

	public boolean remainsLastReviewer() {
		return approved.size() + 1 == reviewers.size();
	}

	public boolean isRejected() {
		return isRejected;
	}

	public String getId() {
		return id;
	}

	public String getTaskId() {
		return taskId;
	}

	public String getSubmitterId() {
		return submitterId;
	}

	public String getName() {
		return name;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ReviewValues that = (ReviewValues) o;
		return isRejected == that.isRejected && id.equals(that.id) && name.equals(that.name) && taskId.equals(that.taskId) && submitterId.equals(that.submitterId) && approved.equals(that.approved) && reviewers.equals(that.reviewers);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id, name, taskId, submitterId, approved, reviewers, isRejected);
	}

	@Override
	public String toString() {
		return "ReviewValues{" +
				"id='" + id + '\'' +
				", name='" + name + '\'' +
				", taskId='" + taskId + '\'' +
				", submitterId='" + submitterId + '\'' +
				", approved=" + approved +
				", reviewers=" + reviewers +
				", isRejected=" + isRejected +
				'}';
	}
}
